using Game.Core;
using UnityEngine;

namespace Game.Manager
{
	public class CameraManager : Singleton<CameraManager>
	{
		public const float CAMERA_Z = -6f;

		public const float CAMERA_FAR = 15f;

		public Camera camera;

		public Transform cameraTrans;

		public float maxX;

		public float wSize;

		public float pixelWSize;

		public float whScale;

		public CameraManager()
		{
			camera = Camera.main;
			if (camera == null)
			{
				Trace.LogError("请设置主相机对象");
			}
			camera.orthographicSize = 5.12f;
			camera.farClipPlane = 15f;
			cameraTrans = camera.transform;
			whScale = (float)Screen.width / (float)Screen.height;
			wSize = whScale * camera.orthographicSize;
			pixelWSize = wSize * 2f / (float)Screen.width;
		}

		public void ResetPosition()
		{
			SetPosition(wSize, camera.orthographicSize);
		}

		public void SetPositionX(float x)
		{
			SetPosition(CalculateX(x), GetPosition().y);
		}

		public void SetPositionY(float y)
		{
			SetPosition(GetPosition().x, y);
		}

		private void SetPosition(float x, float y)
		{
			cameraTrans.position = new Vector3(x, y, -6f);
			Fire(new BaseEvent("game.camera_pos_change", (cameraTrans.position.x - wSize) / (maxX - wSize)));
		}

		public Vector3 GetPosition()
		{
			return cameraTrans.position;
		}

		public float CalculateX(float x)
		{
			return Mathf.Clamp(x, wSize, maxX);
		}

		public void SetBorder(float maxX)
		{
			this.maxX = maxX - wSize;
		}

		public bool InCamera(Vector3 pos, float errVal)
		{
			if (pos.x > cameraTrans.position.x - wSize - errVal && pos.x < cameraTrans.position.x + wSize + errVal)
			{
				return true;
			}
			return false;
		}

		public void SetCameraFar(float val = 0f)
		{
			if (val > 0f)
			{
				camera.farClipPlane = val;
			}
			else
			{
				camera.farClipPlane = 15f;
			}
		}
	}
}
